package com.ffcs.web.controllers;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ffcs.web.model.dto.BaseResponse;
import com.ffcs.web.services.AuthCodeService;
import com.ffcs.web.services.CounselorService;
import com.ffcs.web.services.SMSSenderService;

/**
 * 生成4位数验证码
 * @author Administrator
 *
 */
@Controller
public class CheckAuthCodeController {

	private Logger logger = Logger.getLogger(AdvertController.class);

	@Autowired
	private SMSSenderService sMSSenderService;
	@Autowired
	private AuthCodeService authCodeService;
	@Autowired
	private CounselorService counselorService;
	
	/**
	 * 发送验证码
	 * @return
	 */
	@RequestMapping("isAuthCode")
	@ResponseBody
	public BaseResponse isAuthCode(HttpServletRequest request,String telphone,String code){
		BaseResponse baseResponse=new BaseResponse();
		
		if(null==telphone || "".equals(telphone)){
			baseResponse.setResultCode("-1");
			baseResponse.setResultDesc("电话号码不可为空");
			return baseResponse;
		}
		
		if(null==code || "".equals(code)){
			baseResponse.setResultCode("-1");
			baseResponse.setResultDesc("验证码不可为空");
			return baseResponse;
		}	
		if(!authCodeService.isAuthCode(telphone, code)){
			baseResponse.setResultCode("-1");
			baseResponse.setResultDesc("验证码失效");
			return baseResponse;
		}
		
		baseResponse.setResultCode("0");
		baseResponse.setResultDesc("success");
		return baseResponse;
	}
	
	
	/**
	 * 发送验证码
	 * @return
	 */
	@RequestMapping("isAuthCodeOrder")
	@ResponseBody
	public BaseResponse isAuthCodeOrder(HttpServletRequest request,String telphone,String code){
		BaseResponse baseResponse=new BaseResponse();
		
		if(null==telphone || "".equals(telphone)){
			baseResponse.setResultCode("-1");
			baseResponse.setResultDesc("电话号码不可为空");
			return baseResponse;
		}
		
		if(null==code || "".equals(code)){
			baseResponse.setResultCode("-1");
			baseResponse.setResultDesc("验证码不可为空");
			return baseResponse;
		}	
		if(!authCodeService.isAuthCode(telphone, code)){
			baseResponse.setResultCode("-1");
			baseResponse.setResultDesc("验证码失效");
			return baseResponse;
		}
		baseResponse.setResultCode("0");
		baseResponse.setResultDesc("success");
		return baseResponse;
	}
	
	/**
	 * 发送验证码
	 * @return
	 */
	@RequestMapping("sendAuthCode")
	@ResponseBody
	public BaseResponse sendAuthCode(HttpServletRequest request,String telphone,String flag){
		BaseResponse baseResponse=new BaseResponse();
		
		if(null==telphone || "".equals(telphone)){
			baseResponse.setResultCode("-1");
			baseResponse.setResultDesc("telphone不可为空");
			return baseResponse;
		}
		
		if("upp".equals(flag)){
			if(counselorService.isRegist(telphone)){
				baseResponse.setResultCode("-1");
				baseResponse.setResultDesc("手机号码未注册过");
				return baseResponse;
			}
		}
		
		if("reg".equals(flag)){
			if(!counselorService.isRegist(telphone)){
				baseResponse.setResultCode("-1");
				baseResponse.setResultDesc("手机号码已被注册");
				return baseResponse;
			}
		}
		
//		if(!printHeaderIp(request)){
//			baseResponse.setResultCode("-1");
//			baseResponse.setResultDesc("ip非法，请联系管理员");
//			return baseResponse;
//		}
		String code = "";
		while(code.length()<4)
		code+=(int)(Math.random()*10);
		authCodeService.save(telphone,code);
		sMSSenderService.sendSMS(telphone,"", new String[]{code,"5"});	
		baseResponse.setResultCode("0");
		baseResponse.setResultDesc("success");
		return baseResponse;
	}
	
	public boolean printHeaderIp(HttpServletRequest request){
		
	    String ip = request.getHeader("x-forwarded-for");
	    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
	        ip = request.getHeader("Proxy-Client-IP");
	    }
	    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
	        ip = request.getHeader("WL-Proxy-Client-IP");
	    }
	    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
	        ip = request.getRemoteAddr();
	    }
	    
	    logger.info("***********************[IP:"+ip+"]正在调佐心发送验证码接口*****************************");
	    if(ip.contains("127.0.0.1")){
	    	return true;
	    }
	    return false;
	}
}
